﻿using Ivi.Visa.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Test_Libscvs.Instrument
{
    public class Load_Set
    {
        public FormattedIO488 Load_IO = new FormattedIO488();
        public bool _enaSimulate = false; //用来表示仪表是否运行于offline模式下
        public string mode;
        public string Load_initial(string addr, bool simu, bool reset)
        {
            ResourceManager rm = new ResourceManager();
            //Load_IO = new FormattedIO488();
            string idn = null;
            try
            {
                Load_IO = new Ivi.Visa.Interop.FormattedIO488();
                if (!simu)
                {
                    Load_IO.IO = (IMessage)rm.Open(addr, AccessMode.NO_LOCK, 10000, "");
                    Load_IO.IO.Timeout = 5000;
                    Load_IO.WriteString("*IDN?", true); //Get the IDN string
                    idn = Load_IO.ReadString();
                    Load_IO.WriteString("SYSTem:REMote");

                    if (reset)
                        Load_IO.WriteString("*RST", true);   //Reset the device
                    Thread.Sleep(50);
                    return "Load" + idn;
                }
                else
                {
                    _enaSimulate = true;
                    return "Simulate";
                }
            }
            catch (Exception)
            {
                return "Load Connect Fail"; //连接失败或者仿真的状态，分别返回不同的字符串
            }
        }
        public void close_session()
        {
            if (!_enaSimulate)
            {
                Load_IO.WriteString("SYSTem:LOCal");
                Thread.Sleep(10);
                Load_IO.IO.Close();
            }
        }
        public double[] Meas_CC(double inputcurr)
        {
            double[] output = new double[2];
            //设置输入恒定电流,并输入
            double curr = inputcurr / 1000;
            Control_I(curr);
            //power on
            Load_IO.WriteString("INP 1");
            Thread.Sleep(100);
            //测量得到结果
            Load_IO.WriteString("MEAS:CURR?");
            Thread.Sleep(100);
            output[0] = Convert.ToDouble(Load_IO.ReadString());
            Load_IO.WriteString("MEAS:VOLT?");
            Thread.Sleep(100);
            output[1] = Convert.ToDouble(Load_IO.ReadString());
            Load_IO.WriteString("INP 0");
            return output;
        }
        public void INPON()
        {
            Load_IO.WriteString("INP 1");
            Thread.Sleep(100);
        }
        public void INPOFF()
        {
            Load_IO.WriteString("INP 0");
            Thread.Sleep(100);
        }
        public double[] Meas_CC()
        {
            double[] output = new double[2];
            Load_IO.WriteString("MEAS:CURR?");
            Thread.Sleep(200);
            output[0] = Convert.ToDouble(Load_IO.ReadString()) * 1000;
            Load_IO.WriteString("MEAS:VOLT?");
            Thread.Sleep(200);
            output[1] = Convert.ToDouble(Load_IO.ReadString()) * 1000;
            return output;
        }
        public double[] Meas_CC(double[] inputcurr)
        {
            double[] output = new double[2];
            for (int i = 0; i < inputcurr.Length; i++)
            {
                //设置输入恒定电流,并输入

                double curr = inputcurr[i] / 1000;
                Control_I(curr);
                //power on
                Load_IO.WriteString("INP 1");
                Thread.Sleep(1000);
                //测量得到结果
                Load_IO.WriteString("MEAS:CURR?");
                Thread.Sleep(1000);
                output[2 * i] = Convert.ToDouble(Load_IO.ReadString());
                Load_IO.WriteString("MEAS:VOLT?");
                Thread.Sleep(1000);
                output[2 * i + 1] = Convert.ToDouble(Load_IO.ReadString());
                Load_IO.WriteString("INP 0");
            }
            return output;
        }
        public void SetProt(double Curr_Prot, bool isCurrProt, double Power_Port, bool isPowerProt)
        {
            if (isCurrProt)
            {
                Load_IO.WriteString("CURR:PROT " + Curr_Prot);
            }

            if (isPowerProt)
            {
                Load_IO.WriteString("POW:PROT " + Power_Port);
            }
        }

        public void Setmode(string _mode)
        {
            if (_mode == "CV")
            {
                Load_IO.WriteString("MODE VOLT");//模式设置
            }
            else if (_mode == "CC")
            {
                Load_IO.WriteString("MODE CURR");//模式设置
            }
            else if (_mode == "CP")
            {
                Load_IO.WriteString("MODE POW");//模式设置
            }
            else if (_mode == "CR")
            {
                Load_IO.WriteString("MODE RES");//模式设置
            }
        }
        public void Setmode(string _mode, double input_value)//设置模式的同时输入
        {
            if (_mode == "CV")
            {
                Load_IO.WriteString("MODE VOLT");//模式设置
                Load_IO.WriteString("VOLT " + input_value);
                WaitUntilFinish();
            }
            else if (_mode == "CC")
            {
                Load_IO.WriteString("MODE CURR");//模式设置
                Load_IO.WriteString("CURR " + input_value);
                WaitUntilFinish();
            }
            else if (_mode == "CP")
            {
                Load_IO.WriteString("MODE POW");//模式设置
                Load_IO.WriteString("POW " + input_value);
                WaitUntilFinish();
            }
            else if (_mode == "CR")
            {
                Load_IO.WriteString("MODE RES");//模式设置
                Load_IO.WriteString("RES " + input_value);
                WaitUntilFinish();
            }
        }
        public void Control_I(double input_value)
        {
            Load_IO.WriteString("CURR " + input_value);
            WaitUntilFinish();
        }
        public void Control_V(string input_value)
        {
            Load_IO.WriteString("VOLT " + input_value);
            WaitUntilFinish();
        }
        public void Control_P(string input_value)
        {
            Load_IO.WriteString("POW " + input_value);
            WaitUntilFinish();
        }
        public void Control_R(string input_value)
        {
            Load_IO.WriteString("RES " + input_value);
            WaitUntilFinish();
        }
        public double[] MEAS_ALL()//0:I,1:V;2:P;3:R
        {
            double[] output = new double[4];
            Load_IO.WriteString("INP 1");
            Thread.Sleep(1000);
            Load_IO.WriteString("MEAS:CURR?");
            Thread.Sleep(1000);
            output[0] = Convert.ToDouble(Load_IO.ReadString());
            Load_IO.WriteString("MEAS:VOLT?");
            Thread.Sleep(1000);
            output[1] = Convert.ToDouble(Load_IO.ReadString());
            Load_IO.WriteString("MEAS:POWer?");
            Thread.Sleep(1000);
            output[2] = Convert.ToDouble(Load_IO.ReadString());
            Load_IO.WriteString("MEAS:RES?");
            Thread.Sleep(1000);
            output[3] = Convert.ToDouble(Load_IO.ReadString());
            Load_IO.WriteString("INP 0");
            return output;
        }
        public bool WaitUntilFinish()
        {
            string dmy = null;
            int opcnum = 0;
            Load_IO.WriteString("*OPC?", true);
            dmy = Load_IO.ReadString();
            while (dmy != "1\n")
            {
                dmy = Load_IO.ReadString();
                Thread.Sleep(10);
                opcnum += 1;
                if (opcnum > 2000)
                {
                    return false;
                }
            }
            return true;
        }

    }
}
